package org.delay;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * @Classname DelayQueueMessageProducer
 * @Description 延迟队列生产者
 * @Date 2022/9/14 10:12
 * @Created by wangchangjiu
 */
@Slf4j
public class DelayQueueMessageProducer {

    private DelayQueueMessage delayQueueMessage;

    private DelayQueueTopicRegistrar delayQueueTopicRegistrar;

    public DelayQueueMessageProducer(DelayQueueMessage delayQueueMessage, DelayQueueTopicRegistrar delayQueueTopicRegistrar){
        this.delayQueueMessage = delayQueueMessage;
        this.delayQueueTopicRegistrar = delayQueueTopicRegistrar;
    }

    public <T> void sendMessage(String topic, T jsonSerializableObject, long delay, TimeUnit timeUnit) {
        delayQueueMessage.addDelayQueue(jsonSerializableObject, delay, timeUnit, topic);
        log.info("添加消息：{} 进入 topic ：{} 延迟队列，delay:{} ,timeUnit:{}", JSON.toJSONString(jsonSerializableObject), topic, delay, timeUnit);

        delayQueueTopicRegistrar.registrar(topic);
    }
}
